/*
 * ShiftRegControl.c
 *
 *  Created on: 22.02.2012
 *      Author: winkens
 */
#include <avr/io.h>
#include <stdlib.h>
#include <string.h>
#include "Globals.h"
#include "HwAbstraction.h"
#include "ShiftRegControl.h"

typedef enum
{
  No_Update = 0,
  Update    = 1
} T_SrcUpdate;

typedef struct T_SrcData
{
  T_ShiftValues Shift_t;
  uint8_t       Update_u8;
} T_SrcData;

static T_SrcData SrcData_t;

void SrcSetValues( T_ShiftValues* iValues_t )
{
 if( 0 != memcmp( iValues_t, &SrcData_t.Shift_t, sizeof(iValues_t) ) )
 {
   //UART_PUT_VALUE("iValue: ", *iValues_t[0]);
   memcpy(&SrcData_t.Shift_t, iValues_t, sizeof(SrcData_t.Shift_t) );
   SrcData_t.Update_u8 = Update;
 }
}

void SrcInit( void )
{
  memset( &SrcData_t, 0, sizeof(SrcData_t) );
}

void SrcCyclic( void )
{
  if( Update == SrcData_t.Update_u8 )
  {
    int8_t lCount_s8 = 0;
    //UART_PUT_VALUE("ShiftData: ", SrcData_t.Shift_t[0] );
    for( lCount_s8 = SRC_NO_OF_BITS_U8-1; lCount_s8 >= 0 ; lCount_s8-- )
    {
      uint8_t lValue_u8 = 0;
      /* determine current shift value */
      lValue_u8 = (0 != (SrcData_t.Shift_t[lCount_s8/8] & 1 << (lCount_s8%8)));
      //UART_PUT_VALUE( "Shift", lValue_u8 );
      /* output of value */
      if( 0 != lValue_u8 )
      {
        SET_DS_PIN;
      }
      else
      {
        CLR_DS_PIN;
      }
      /* generate pulse on shift register clock input*/
      SET_SHCP_PIN;
      /* check if nop is necessary or not */

      CLR_SHCP_PIN;
      /* clear data pin */
      CLR_DS_PIN;
    }
    SET_STCP_PIN;
    CLR_STCP_PIN;
    SrcData_t.Update_u8 = No_Update;
  }
}
